package yunplus.io.hchome.display.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yunplus.io.hchome.R;
import yunplus.io.hchome.display.utils.ToastUtils;
import yunplus.io.hchome.display.view.ILoginView;
import yunplus.io.hchome.display.view.IWebAppView;
import yunplus.io.hchome.presenter.contract.ILoginPresenter;
import yunplus.io.hchome.presenter.implement.LoginPresenter;

public class LoginActivity extends BaseActivity implements ILoginView{

    //------------------ private const define ----------------------
    private static final String TAG = "LoginActivity";

    @BindView(R.id.edt_domain)
    EditText _mEdtDomain;

    @BindView(R.id.edt_username)
    EditText _mEdtUsername;

    @BindView(R.id.edt_password)
    EditText _mEdtPassword;

    @BindView(R.id.chb_lan)
    CheckBox _mChbLan;

    ProgressDialog _mProgressDialog;

    private String _domain, _username, _password;

    private boolean _isWifi = false;

    private ILoginPresenter _mLoginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        _isWifi = getIntent().getBooleanExtra(ILoginView.INTENT_EXTRA_KEY_NETWORK, true);
        if(_isWifi){
            _mChbLan.setVisibility(View.VISIBLE);
        }
        _mLoginPresenter = new LoginPresenter(this, this);
        _mLoginPresenter.open();
    }

    @OnClick(R.id.btn_login)
    void onLoginClick(){
        _domain = _mEdtDomain.getText().toString();
        _domain = _domain.replace("：", ":");
        _username = _mEdtUsername.getText().toString();
        _password = _mEdtPassword.getText().toString();
        _mLoginPresenter.login(_domain, _username, _password, _mChbLan.isChecked());
    }

    //---------- login -------------
    @Override
    public void onLoginStart() {
        Log.d(TAG, "onLoginStart");
        _mProgressDialog = new ProgressDialog(this);
        _mProgressDialog.setMessage("正在登录...请稍后");
        _mProgressDialog.setCancelable(false);
        _mProgressDialog.show();
    }

    @Override
    public void onLoginError(String message) {
        Log.d(TAG, message);
        _mProgressDialog.dismiss();
        ToastUtils.with(this).show(message);
    }

    @Override
    public void onLoginOkFromLan(boolean isLan) {
        Log.d(TAG, "onLoginFinished");
        _mProgressDialog.dismiss();
        Intent intent = new Intent(this, WebviewActivity.class);
        intent.putExtra(IWebAppView.INTENT_EXTRA_KEY_LAN, isLan);
        intent.putExtra(IWebAppView.INTENT_EXTRA_KEY_DOMAIN, _domain);
        startActivity(intent);
        finish();
    }

}

